﻿<template>
	<form-container title="新增批量配置" :width="700" :visible="visible" :destroy-on-close="true" @close="onClose">
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="唯一编码" name="code">
				<a-input v-model:value="formData.code" placeholder="请输入唯一编码" />
			</a-form-item>
			<a-form-item label="选择表：" name="dbTable">
				<a-select
					v-model:value="formData.dbTable"
					:options="tableList"
					style="width: 100%"
					placeholder="请选择表"
					@select="formFieldAssign(formData.dbTable)"
				>
				</a-select>
			</a-form-item>
			<a-form-item label="所属库" name="configId">
				<a-input disabled v-model:value="formData.configId" placeholder="请输入所属库" />
			</a-form-item>
			<a-form-item label="实体名" name="entityName">
				<a-input disabled v-model:value="formData.entityName" placeholder="请输入实体名" />
			</a-form-item>
			<a-form-item label="表名" name="tableName">
				<a-input disabled v-model:value="formData.tableName" placeholder="请输入表名" />
			</a-form-item>
			<a-form-item label="表描述" name="tableDescription">
				<a-input disabled v-model:value="formData.tableDescription" placeholder="请输入表描述" />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</form-container>
</template>

<script setup name="batchEditForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import batchEditApi from '@/api/sys/batchEditApi'

	const tableList = ref([])
	// 抽屉状态
	const visible = ref(false)
	// 定义emit事件
	const emit = defineEmits({ successful: null })
	//表单
	const formRef = ref()
	// 表单数据，也就是默认给一些数据
	const formData = ref({})
	//提交等待
	const submitLoading = ref(false)

	// 打开抽屉
	const onOpen = () => {
		visible.value = true //显示抽屉
		//表单数据
		formData.value = {}
		// 获取数据库中的所有表
		batchEditApi.basicTables().then((data) => {
			tableList.value = data.map((item) => {
				return {
					value: item['tableName'],
					configId: item['configId'],
					label: `${item['tableDescription']}-${item['configId']}-${item['entityName']}`,
					entityName: item['entityName'],
					tableName: item['tableName'],
					tableDescription: item['tableDescription'] || item['entityName'],
					tableColumns: []
				}
			})
		})
	}

	// 表单内设置默认的值
	const formFieldAssign = (value) => {
		const data = tableList.value.find((item) => item.value === value)
		formData.value.tableName = data.tableName
		formData.value.tableDescription = data.tableDescription
		formData.value.entityName = data.entityName
		formData.value.configId = data.configId
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}

	// 默认要校验的
	const formRules = {
		code: [required('请输入唯一编码')],
		configId: [required('请输入所属库')],
		entityName: [required('请输入实体名')],
		tableName: [required('请输入表名')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			batchEditApi
				.batchEditSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}

	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
